package com.kaifamiao.wendao.servlet;

import com.kaifamiao.utils.GraphicHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

@WebServlet( "/captcha/image/*" )
public class CaptchaImageServlet extends HttpServlet {
    @Override
    protected void doGet( HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {

        HttpSession session = request.getSession();
        // 获得可以向 响应对象 中输出字节数据的 输出流
        OutputStream output = response.getOutputStream();
        // 随机生成验证码字符串并根据生成的验证码字符串创建一副图片，再将图片输出到 output 对应的字节输出流
        final String code = GraphicHelper.create( 6 , false , 180 , 50 , output , 50 );
        System.out.println( "验证码字符串: " + code );
        // 将本次产生的验证码字符串保存到会话对象中，以便于完成数据校验
        session.setAttribute( "CAPTCHA" , code );
    }
}
